{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0cb7330e",
   "metadata": {},
   "source": [
    "________________\n",
    "* 2022/10/24/week10\n",
    "* Python学习记录\n",
    "________________"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3f55eee",
   "metadata": {},
   "source": [
    "# 本周学习目标\n",
    "> 1. while循环\n",
    "> 2. 购物车实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d53e907",
   "metadata": {},
   "source": [
    "## 1. while循环\n",
    "\n",
    "* 回顾: for 循环: for + range()可以指定循环的次数--->关于网页URL队列的抓取实践\n",
    "* while循环：当某种条件成立时，停止循环\n",
    "* while True : 无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a60f2c9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人哪有不疯的啊？ 0\n"
     ]
    }
   ],
   "source": [
    "# 1. 指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"人哪有不疯的啊？\",count)\n",
    "    count = count+1\n",
    "    \n",
    "    break\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "37a19542",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人哪有不疯的啊？ 0\n",
      "人哪有不疯的啊？ 1\n",
      "人哪有不疯的啊？ 2\n",
      "人哪有不疯的啊？ 3\n",
      "人哪有不疯的啊？ 4\n",
      "人哪有不疯的啊？ 5\n",
      "人哪有不疯的啊？ 6\n",
      "人哪有不疯的啊？ 7\n",
      "人哪有不疯的啊？ 8\n",
      "人哪有不疯的啊？ 9\n"
     ]
    }
   ],
   "source": [
    "# 1. 指定次数\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"人哪有不疯的啊？\",count)\n",
    "    count = count+1\n",
    "    if count == 10:\n",
    "        break # break 终止循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d87628b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号: sfa\n",
      "密码: f1s\n",
      "账号不存在或密码不正确！请重新输入！您还有2次尝试的机会\n",
      "账号: dxqwa\n",
      "密码: as\n",
      "账号不存在或密码不正确！请重新输入！您还有1次尝试的机会\n",
      "账号: dfasfd\n",
      "密码: ds\n",
      "账号不存在或密码不正确！请1小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 2. while True循环，可以让用户重复输入\n",
    "user01 = \"xuanyi\"\n",
    "password01 = \"1111\"\n",
    "count = 0 # 计数器\n",
    "while True:\n",
    "    username = input(\"账号: \")\n",
    "    password = input(\"密码: \")\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登陆成功~欢迎\"+user01)\n",
    "        break # 中止循环\n",
    "    # 情景二：帐号不存在或密码不正确，用户可以输入三次，三次之后1小时之后再来尝试\n",
    "    else:\n",
    "        if count<2:\n",
    "            print(\"账号不存在或密码不正确！请重新输入！您还有\"+str(2-count)+\"次尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确！请1小时后再来尝试\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e442c3cf",
   "metadata": {},
   "source": [
    "## 2. 购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "2c6d8a30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9f7318a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：20\n",
      "----------商品列表----------\n",
      "商品序号: (0)： 冰鲜柠檬水 4元\n",
      "商品序号: (1)： 蜜桃四季春 6元\n",
      "商品序号: (2)： 雪王雪顶咖啡 6元\n",
      "商品序号: (3)： 草莓摇摇奶昔 6元\n",
      "请输入商品编号：0\n",
      "商品购买成功，正在制作中...您的余额还有16元\n",
      "----------商品列表----------\n",
      "商品序号: (0)： 冰鲜柠檬水 4元\n",
      "商品序号: (1)： 蜜桃四季春 6元\n",
      "商品序号: (2)： 雪王雪顶咖啡 6元\n",
      "商品序号: (3)： 草莓摇摇奶昔 6元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有10元\n",
      "----------商品列表----------\n",
      "商品序号: (0)： 冰鲜柠檬水 4元\n",
      "商品序号: (1)： 蜜桃四季春 6元\n",
      "商品序号: (2)： 雪王雪顶咖啡 6元\n",
      "商品序号: (3)： 草莓摇摇奶昔 6元\n",
      "请输入商品编号：q\n",
      "您已购买商品： [['冰鲜柠檬水', 4], ['雪王雪顶咖啡', 6]]\n",
      "您的余额还有10元，欢迎下次光临~~~\n"
     ]
    }
   ],
   "source": [
    "# 1. 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "product_list = [\n",
    "    [\"冰鲜柠檬水\",4],\n",
    "    [\"蜜桃四季春\",6],\n",
    "    [\"雪王雪顶咖啡\",6],\n",
    "    [\"草莓摇摇奶昔\",6]\n",
    "]\n",
    "shopping_car = []\n",
    "\n",
    "AliPay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"----------商品列表----------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号:\",\"(\"+str(i)+\")：\",product_list[i][0],str(product_list[i][1])+\"元\")  # 格式化输出\n",
    "        # print(product_list[1][0],str(product_list[1][1])+\"元\")\n",
    "        # print(product_list[2][0],str(product_list[2][1])+\"元\")\n",
    "        # print(product_list[3][0],str(product_list[3][1])+\"元\")\n",
    "\n",
    "    # 2. 让用户输入商品编号进行商品的购买。:直接用商品列表的 index 作为商品标号\n",
    "\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if AliPay >= product_list[int(user_choice)][1]:\n",
    "            AliPay = AliPay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])  #----------\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(AliPay))\n",
    "\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~~\")\n",
    "    # 情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买商品：\",shopping_car) #----------\n",
    "        print(\"您的余额还有%s元，欢迎下次光临~~~\"%(AliPay)) #----------\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b12d7c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 假设用户选择了0\n",
    "product_list[0][1]\n",
    "# 用户选择的商品价格： product_list[user_choice][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d6405d2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
